<template>
	<li id="getcode" class="my-flex-center" :class="[{'disabled':time>0}]" v-on:click="run" :disabled="time>0">
		{{text}}
	</li>
</template>

<script>
	export default {
		props: {
			tel: {
				type: String,
				default: null
			},
			realName:{
				type: String,
				default: null
			},
			userType: {
				type: String,
				default: ''
			},
			userCurrentType:{
				type: Number,
				default: ''
			},
			second: {
				type: Number,
				default: 60
			},
			type: {
				type: Number,
				default: 1
			},
			disabled: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				time: 0,
				getStatus: false,
				bosStatus:false
			}
		},
		computed: {
			text() {
				return this.time > 0 ?  '重新获取 ' +  this.time + 's' : '获取短信验证码';
			}
		},
		mounted() {
//			console.log(this.userCurrentType);
			console.log(this.userType);
		},
		methods: {
			async run() {
				if(this.userType!='personRegister'){
					let rule = [{
					name: 'tel',
					label: '手机号',
					rule: ['validateTel_']
					},{
						name: 'realName',
						label: '企业全称',
						rule: ['validateEmpty_']
					}]
					let obj = {
						tel: this.tel,
						userName:this.realName,
						bos:false
					};
					if(!this.validatetool.validate(this, obj, rule)) {
						return;
					}
				}else{
					let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
					if(!this.tel) {
						this.$message.error('请输入手机号');
						return
					}else if(!myreg.test(this.tel)){
						this.$message.error('手机号格式不正确！');
						return
					}
				}
				if(this.time > 0) {
					return;
				}	
				let params={};
				if(this.userType=='personRegister'){
					params.bos=true;
				}else{
					params.bos=false;
				}
				params.type=this.type;
				params.phone=this.tel;
				params.userName=this.realName;
				params.userType=this.userType;
				let res= await this.httptool.post(this.apis.sendAliyunCode,params);
				this.$message(res.msg)
				this.start(); //启动倒计时	
			},
			start() {
				this.time = this.second;
				this.timer();
			},
			timer() {
				if(this.time > 0) {
					this.time--;
					setTimeout(this.timer, 1000);
				}
			}
		},

	};
</script>
<style lang="less">
	@import "../../less/base/base.less";
	#getcode {
		display:inline-block;
		text-align:center;
		font-size: 12px;
		height:34px;
		width:100px;
		border:none;
		outline:none;
		background: rgb(255, 81, 81);
		color: #fff;
		border-radius: 4px;
		cursor:pointer;
		&.disabled {
			background: #999999;
		}
	}
</style>